package com.huaishushu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaishushu.common.CustomException;
import com.huaishushu.dto.DishDto;
import com.huaishushu.dto.SetmealDto;
import com.huaishushu.entity.*;
import com.huaishushu.mapper.SetmealMapper;
import com.huaishushu.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author ChiBaoBaoHuaiShuShu
 * @since 2023-05-28
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements ISetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private ISetmealDishService setmealDishService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IDishService dishService;

    @Autowired
    private IDishFlavorService dishFlavorService;


    /**
     * 添加套餐信息
     * 操作两张表，setmeal_dish和setmeal表
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public void saverWithDish(SetmealDto setmealDto) {
        //保存基本的套餐信息setmeal
        setmealMapper.insert(setmealDto);
        //SetmealDishes中缺少setmealId，需要为其赋值
        Long setmealId = setmealDto.getId();
        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
            setmealDish.setSetmealId(String.valueOf(setmealId));
        }
        //保存套餐中所对于的菜品信息setmeal_dish
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());

    }

    /**
     * 菜品信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> page(Integer page, Integer pageSize, String name) {
        //构造Setmeal的分页构造器
        Page<Setmeal> setmealPage = new Page<>();
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNoneEmpty(name), Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealMapper.selectPage(setmealPage, lqw);

        //构建SetmealDto的分页构造器
        Page<SetmealDto> setmealDtoPage = new Page<>();
        //对象拷贝，但不拷贝“records”
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtoList = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item, setmealDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            //此处是因为当我们自己添加或通过sql文件导入菜品时并没有设置分类，所有可能是会为空的
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtoList);
        return setmealDtoPage;
    }

    /**
     * 根据id数组来批量删除套餐信息
     * @param ids
     */
    @Override
    @Transactional
    public void delete(String[] ids) {
        for (String id : ids) {
            //判断当前套餐是否在售卖，如果在售卖则抛出一个业务异常
            Setmeal setmeal = setmealMapper.selectById(id);
            if (setmeal.getStatus() == 1) {
                throw new CustomException("套餐正在售卖，无法删除，售卖的套餐名称为：" + setmeal.getName());
            }
            //删除套餐的基本信息
            setmealMapper.deleteById(id);

            //删除套餐关联的菜品信息
            LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(lqw);
        }
    }

    /**
     * 根据套餐id来套餐的基本信息以及套餐所对应的菜品信息
     * @param setmealId
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long setmealId) {
        //查询套餐的基本信息，从setmeala表中查询
        Setmeal setmeal = setmealMapper.selectById(setmealId);

        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        //查询当前套餐所对应的菜品信息，从setmeal_dish表查询
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> dishes = setmealDishService.list(lqw);
        setmealDto.setSetmealDishes(dishes);

        return setmealDto;
    }

    /**
     * 修改套餐信息，操作的是两张表，还需要插入菜品信息
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //修改基本的套餐信息setmeal
        setmealMapper.updateById(setmealDto);

        //清理当前菜品对应口味数据--dish_flavor表的delete操作
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(lqw);

        //SetmealDishes中缺少setmealId，需要为其赋值
        Long setmealId = setmealDto.getId();
        for (SetmealDish setmealDish : setmealDto.getSetmealDishes()) {
            setmealDish.setSetmealId(String.valueOf(setmealId));
        }
        //保存套餐中所对于的菜品信息setmeal_dish
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    /**
     * 根据传递回来的status来判断是启用还是修改操作
     * 之后根据id来批量修改
     * @param status
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void updateStatusById(Integer status, String[] ids) {
        //禁售业务操作
        if (status == 0) {
            for (String id : ids) {
                setmealMapper.updateStatusById(0, Long.valueOf(id));
            }
        }
        //起售业务操作
        if (status == 1) {
            for (String id : ids) {
                setmealMapper.updateStatusById(1, Long.valueOf(id));
            }
        }
    }

    /**
     * 展示前台的套餐中的所有套餐信息以及套餐中对应的菜品信息。
     * 此处返回值类型是List<Setmeal>也是可以的，但是我写成这样的目的是因为方便以后扩展做准备，套餐管理页面显示所有套餐信息以及套餐所对应的菜品信息。
     * @param setmeal
     * @return
     */
    @Override
    public List<SetmealDto> listSetmealDto(Setmeal setmeal) {
        //先根据分类id来查询套餐的基本信息
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        lqw.eq(Setmeal::getStatus, setmeal.getStatus());
        List<Setmeal> setmealList = setmealMapper.selectList(lqw);
        //开始为SetmealDto赋值
        List<SetmealDto> setmealDtoList = setmealList.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            //将Setmeal的基本信息拷贝给setmealDto
            BeanUtils.copyProperties(item, setmealDto);
            //根据套餐的id来查询套餐中菜品的信息，并将其赋值给SetmealDto中的List<SetmealDish> setmealDishes
            Long setmealId = item.getId();
            LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealId);
            List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
            setmealDto.setSetmealDishes(setmealDishList);

            return setmealDto;
        }).collect(Collectors.toList());

        return setmealDtoList;
    }

    /**
     * 前台查看套餐信息中的菜品信息
     * 根据套餐id来查询菜品的信息
     * @param setmealId
     * @return
     */
    @Override
    public List<DishDto> getListDishDtoById(Long setmealId) {
        //查询当前套餐中所对应的菜品信息(setmeal_dish表)
        LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetmealDish::getSetmealId, setmealId);
        List<SetmealDish> setmealDishList = setmealDishService.list(lqw);

        List<DishDto> dishDtoList = setmealDishList.stream().map(item -> {
            DishDto dishDto = new DishDto();
            //通过菜品id查询基本的菜品信息
            String dishId = item.getDishId();
            Dish dish = dishService.getById(dishId);
            BeanUtils.copyProperties(dish, dishDto);
            //将套餐中菜品所对应的份数赋值给Dto
            dishDto.setCopies(item.getCopies());

            //查询菜品所对应的口味信息并赋值给Dto
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, dishId);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(dishFlavorList);

            return dishDto;
        }).collect(Collectors.toList());

        return dishDtoList;
    }


}
